package com.qxf.util.db;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;

public class PageDivision {
	private int begin;
	private int end;
	private int prePage;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int totalSize;
	private int pageSize;
	private int pageListSize = 8;
	private int pageListStart;
	private int pageListEnd;

	
	public int getPageListStart() {
		return pageListStart;
	}

	public void setPageListStart(int pageListStart) {
		this.pageListStart = pageListStart;
	}

	public int getPageListEnd() {
		return pageListEnd;
	}

	public void setPageListEnd(int pageListEnd) {
		this.pageListEnd = pageListEnd;
	}

	public PageDivision(){}
	
	public PageDivision(int currentPage, int pageSize, int totalSize){
		this.totalSize = totalSize;
		this.pageSize = pageSize;
		calculatePage(currentPage);
	}
	
	public PageDivision(int currentPage, int pageSize, String countSql){
		this.totalSize = getTotalSizeFromSql(countSql);
		this.pageSize = pageSize;
		calculatePage(currentPage);
	}
	
	private void calculatePage(int currentPage){
		this.totalPage = this.pageSize == 0 ? 0 : ((this.totalSize - 1)  / this.pageSize + 1);
		this.currentPage = currentPage < 0 ? 0 : (currentPage > this.totalPage ? this.totalPage : currentPage);
		this.prePage = (this.currentPage <= 1) ? this.currentPage : (this.currentPage - 1);
		this.nextPage = (this.currentPage == this.totalPage) ? this.currentPage : (this.currentPage + 1);
		this.pageListStart = this.currentPage / this.pageListSize * this.pageListSize + 1;
		this.pageListEnd = (this.pageListStart + this.pageListSize - 1) > this.totalPage ? this.totalPage : (this.pageListStart + this.pageListSize - 1);
		this.begin = this.totalSize == 0 ? 0 : (this.currentPage-1) * this.pageSize + 1;
		this.end = (this.begin + this.pageSize - 1) > this.totalSize ? this.totalSize : (this.begin + this.pageSize - 1);
	}
	
	@SuppressWarnings("unchecked")
	public static int getTotalSizeFromSql(String countSql){//countSql likes "select * from tb_group"
		SQLProcessor sp = null;
		String sql = "select count(*) as count from (" + countSql + ") as tmptable";
		try{
			sp = new SQLProcessor();
			List<Map<String, Object>> list = sp.executeQuery(sql);
			return (Integer)list.get(0).get("count");
		}catch(Exception e){
			System.out.println("Error SQL: " + sql);
			e.printStackTrace();
		}finally{
			try{
				if(sp!=null)
					sp.close();
			}catch(SQLException se){
				se.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 * show the page division list
	 * @param out
	 * @param url	--->	diarlylist.jsp?para1=x&para2=2&...&currentPage=
	 */
	public void showPageList(JspWriter out, String url) throws IOException{
		out.append("[");
		if(this.getCurrentPage()==1)
			out.append("首 页/上一页");
		else
			out.append("<a href=\"" + url + 1 + "\">首 页</a>/<a href=\"" + url + this.getPrePage() + "\">上一页</a>");
		out.append("]&nbsp;"); 
		for(int i = this.getPageListStart(); i <= this.getPageListEnd(); i++){
			if(i == this.getCurrentPage())
				out.append("<b>" + i + "</b>&nbsp;" );
			else
				out.append("<a href=\"" + url + i + "\" title=\"转到第" + i + "页\">" + i + "</a>&nbsp;");
		}
		out.append("[");
		if(this.getCurrentPage() == this.getTotalPage())
			out.append("下一页/尾 页");
		else
			out.append("<a href=\"" + url + this.getNextPage() + "\">下一页</a>/<a href=\"" + url + this.getTotalPage() + "\">尾 页</a>");
		out.append("]");
		out.flush();
	}
	
	public int getBegin() {
		return begin;
	}

	public int getEnd() {
		return end;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	
	
}
